3 Author: Andrés Mejía-Posada
4 (http://blogaritmo.factorcomun.org)
28 #define _for(i, a, b) for (int i=0, _n = b; i<_n; ++i)
29 #define _from(i, a, b) for (int i=a, _n = b; i>=_n; --i)
31 #define D(x) cout << #x " is " << x << endl
33 struct state
{ int s
[3][3]; };
35 int encode(const state
&s
){ int r
= 0; _for(i
, 0, 3) _for(j
, 0, 3) r
= 10*r
+ s
.s
[i
][j
]; return r
; }
36 void decode(int r
, state
&s
) { _from(i
, 2, 0) _from(j
, 2, 0) s
.s
[i
][j
] = r
%10, r
/=10; }
40 map
<int, string
> choice
;
51 state u
; decode(x
, u
);
53 for(int i
=0, t
; i
<3; ++i
){
55 t
= v
.s
[i
][0], v
.s
[i
][0] = v
.s
[i
][1], v
.s
[i
][1] = v
.s
[i
][2], v
.s
[i
][2] = t
;
57 if (!prev
.count(y
)) prev
[y
] = x
, choice
[y
] = string("H") + char(i
+'1'), q
.push(y
);
61 for(int i
=0, t
; i
<3; ++i
){
63 t
= v
.s
[2][i
], v
.s
[2][i
] = v
.s
[1][i
], v
.s
[1][i
] = v
.s
[0][i
], v
.s
[0][i
] = t
;
65 if (!prev
.count(y
)) prev
[y
] = x
, choice
[y
] = string("V") + char(i
+'1'), q
.push(y
);
71 //assert(freopen("puzzle.in", "r", stdin) != NULL);
73 for (int u
, x
; !(u
= 0);){
74 for (int i
=0; i
<9; ++i
) cin
>> x
, u
= 10*u
+ x
;
76 if (!prev
.count(u
)) cout
<< "Not solvable" << endl
;
79 for (int w
= prev
[u
]; w
!= -1; w
= prev
[u
= w
]) s
+= choice
[u
];
80 cout
<< s
.size() / 2 << " " << s
<< endl
;